// 在这个 JS 文件中，专门来创建 Store 的实例对象
import { observable, action } from 'mobx-miniprogram'

export const store = observable({
  address: '北京匠心之轮国际网球中心（朝阳区）',

  // 小程序系统信息 ----------------------------------
  window_info: {},
  set_window_info: action(function () {
    // 获取微信字体大小
    const appBaseInfo = wx.getAppBaseInfo()
    this.window_info.fontSize = appBaseInfo.fontSizeSetting


    const windowInfo = wx.getWindowInfo()
    const deviceInfo = wx.getDeviceInfo()
    this.window_info.ratio = windowInfo.windowWidth / 750

    // 计算底部
    if (windowInfo.safeArea && windowInfo.safeArea.bottom !== 0) {
      const bottom = windowInfo.screenHeight - windowInfo.safeArea.bottom
      this.window_info.bottom = bottom > 10 ? bottom  : bottom
    } else {
      this.window_info.bottom = 0
    }


    // 计算顶部
        let menuButtonInfo
        if (deviceInfo.system.indexOf('iOS') > -1) {
          // ios设备的胶囊按钮都是固定的
          menuButtonInfo = {
            width: 87,
            height: 32,
            left: windowInfo.screenWidth - 7 - 87,
            right: windowInfo.screenWidth - 7,
            top: windowInfo.statusBarHeight + 4,
            bottom: windowInfo.statusBarHeight + 4 + 32
          }
        } else {
          // 安卓通过api获取
          menuButtonInfo = wx.getMenuButtonBoundingClientRect()
        }
        this.window_info.statusBarHeight = windowInfo.statusBarHeight
        this.window_info.menuHeight = (menuButtonInfo.top - windowInfo.statusBarHeight) * 2 + menuButtonInfo.height


  }),
  // 配置信息 ----------------------------------
  config: {},
  set_config: action(function (value) {
    this.config = value || {}
  }),

  // 用户信息 ----------------------------------
  user_info: {},
  set_user_info: action(function (value, isCover) {
    if (isCover) {
      this.user_info = value || {}
    } else {
      this.user_info = Object.assign({}, this.user_info, value)
    }
  }),

})
